/*
 * app_hub_api_subsidiary.h
 *
 *  Created on: 2023年7月27日
 *      Author: HXF
 */

#ifndef APP_HUB_API_SUBSIDIARY_H_
#define APP_HUB_API_SUBSIDIARY_H_

#include "app_hub_api_def.h"
#include "stdbool.h"

#define FILE_NAME(x) strrchr(x,'/')?strrchr(x,'/')+1:x

//short变量 大小端交换一下
#define B_L_SWAP_16(A)  ((((unsigned short)(A) & 0xff00) >> 8) | \
                            (((unsigned short)(A) & 0x00ff) << 8))


#define    HUB_API_GET_BIT(x, bit)    ((x & (1 << bit)) >> bit)    /* 获取第bit位 */
#define    HUB_API_CLEAN_BIT(x, bit)  (x &= ~(1 << bit))        /* 清零第bit位 */
#define    HUB_API_SET_BIT(x, bit)    (x |= (1 << bit))            /* 置位第bit位 */


unsigned short get_msg_crc16(unsigned char *msg, int len);

unsigned char get_rfmid_from_catno(unsigned char cat, unsigned char no);
unsigned char get_cat_from_rfmid(unsigned char rfmid);
unsigned char get_no_from_cat_rfmid(unsigned char cat, unsigned char rfmid);
bool check_rfmid_is_registered(unsigned char rfmid);
bool check_hub_event_need_rtns(void);
bool check_hub_event_is_finsih(void);
bool check_rfm_event_need_rtns(void);
bool check_rfm_event_is_finsih(void);
bool check_file_event_need_rtns(void);
bool check_file_event_is_finsih(void);

void set_reset_enable(unsigned char state);
void hub_do_reset(void);

int analysis_panel_bc_pkt(const unsigned char *bc_pkt, int bc_pkt_len,
                          unsigned char *result_pkt, int *result_pkt_len);

int analysis_panel_p2p_pkt(const unsigned char *p2p_pkt, int p2p_pkt_len,
                           unsigned char *result_pkt, int *result_pkt_len,
                           unsigned char rfmid);

int analysis_rfm_evt_payload_keyvalue(const unsigned char *payload, int payload_len,
                          unsigned char *result_msg, int *result_msg_len,
                          unsigned char rfmid);

void printf_pkt(const hub_api_usart_msg_t *ptr_pkt);
void hub_api_test(void);


void start_wait_ack_Timer(int time, void *userData);
void stop_wait_ack_Timer(void *userData);

void start_usart_idle_Timer(int time, void *userData);
void stop_usart_idle_Timer(void *userData);
void restart_usart_idle_Timer(int time, void *userData);

void start_avg_rssi_Timer(int time, void *userData);
void start_delay_com_tx_task(int time, void *userData);
void start_fix_10S_Timer(int time, void *userData);
void start_clean_bc_busy_Timer(int time, void *userData);

unsigned char get_month_from_str(char * month);
unsigned short reset_getResetReason(void);

void set_rfm_offline_countdown(unsigned char rfmid);
void set_rfm_offline(unsigned char rfmid);
unsigned char get_rfm_offline_countdown(unsigned char rfmid);
void update_rfm_offline_countdown(unsigned char rfmid);
bool check_rfm_is_offline(unsigned char rfmid);
void creat_rfm_add_cjson(unsigned char rfmid, unsigned char type,
                         signed char rssi, unsigned char battv,
                         unsigned char temp, unsigned char *ver);
void creat_rfm_report_cjson(unsigned char rfmid, unsigned char type,
                            signed char rssi, unsigned char battv,
                            unsigned char temp);

void hub_delay_delete_rfm(void);

#if defined(HUB_AS_TEST_MACHINE) && defined (HUB_DEVICE)
void set_bc_delete(bool is_lock);
bool get_bc_delete(void);
void set_can_delete(bool is_can);
bool get_can_delete(void);
void set_delete_rfmid(unsigned char rfmid);
unsigned char get_delete_rfmid(void);
void start_delete_rfm_timer(int time, void *userData);
#endif
#endif /* APP_HUB_API_SUBSIDIARY_H_ */
